package com.rrd.itext.xmlBean.layoutBean;

import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.rrd.itext.xmlBean.layoutBean.iface.PositionElement;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Table")
public class XmlTable extends XmlColorStyle implements PdfElementRender, PositionElement{
	@XStreamAsAttribute
	@XStreamAlias("numColumns")
	private int numColumns;
	@XStreamAsAttribute
	@XStreamAlias("offsetX")
	private Float offsetX;
	@XStreamAsAttribute
	@XStreamAlias("offsetY")
	private Float offsetY;
	@XStreamAsAttribute
	@XStreamAlias("align")
	private String align;
	@XStreamAsAttribute
	@XStreamAlias("positionType")
	private String positionType;
	@XStreamAsAttribute
	@XStreamAlias("width")
	private Integer width;
	@XStreamAsAttribute
	@XStreamAlias("widths")
	private String widths;
	@XStreamAlias("Trs")
	private List<XmlTr> trs;	
	private int count;
	public int getCount()
	{
		return this.count;
	}
	public String getWidths() {
		return widths;
	}

	public void setWidths(String widths) {
		this.widths = widths;
	}
	
	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}


	public int getNumColumns() {
		return numColumns;
	}

	public void setNumColumns(int numColumns) {
		this.numColumns = numColumns;
	}
	public Float getOffsetX() {
		return offsetX;
	}

	public void setOffsetX(Float offsetX) {
		this.offsetX = offsetX;
	}

	public Float getOffsetY() {
		return offsetY;
	}

	public void setOffsetY(Float offsetY) {
		this.offsetY = offsetY;
	}
	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getPositionType() {
		return positionType;
	}

	public void setPositionType(String positionType) {
		this.positionType = positionType;
	}

	public List<XmlTr> getTrs() {
		return trs==null?new ArrayList<XmlTr>():trs;
	}

	public void setXmlTrs(List<XmlTr> trs) {
		this.trs = trs;
	}
	
	@Override
	public void render(PdfContentByte cb, PDFBuilder builder) throws Exception {
		cb.beginText();
		int col = this.getNumColumns();
		PdfPTable table=null;
		if(col>0)
		{			
			table = new PdfPTable(col);
		}
		List<PdfPCell> list = new ArrayList<PdfPCell>();
			if(this.getWidth() > 0)
			{
				table.setTotalWidth(this.getWidth());
				table.setLockedWidth(true);
			}
			if(this.getWidths()!=null)
			{
				float[] widths=this.getArraytWidths(this.getWidths());
				table.setWidths(widths);									
			}
			List<XmlTr> trs = this.getTrs();
			count = trs.size();
			for(XmlTr tr : trs)
			{
				list = tr.parse(builder);
				for(PdfPCell cell : list)
				{
					table.addCell(cell);
				}
			}
			table.completeRow();
			table.writeSelectedRows(0, -1, this.getOffsetX(), this.getOffsetY(), cb);
			cb.endText();
	}
	
	
	private float[] getArraytWidths(String widths) 
	{
		widths=widths.substring(widths.indexOf("(")+1, widths.indexOf(")"));
		String[] stringWidth  =widths.trim().split(",");
        float[] floatWidth=new float[stringWidth.length];
        for (int i = 0; i < floatWidth.length; i++) 
        {
			 floatWidth[i]=Float.parseFloat(stringWidth[i]);
		}
		return floatWidth;
	}
    @Override
    public Integer getHeight() {
        return 0;
    }
    @Override
    public void setHeight(Integer height) {
        // TODO Auto-generated method stub
        
    }
}
